package net.sunxiaolong.tool.captureinsureknowledge.tool.capture;

import net.sunxiaolong.tool.captureinsureknowledge.entity.CpicKnowledge;
import net.sunxiaolong.tool.captureinsureknowledge.repository.CpicKnowledgeRepository;
import net.sunxiaolong.tool.captureinsureknowledge.tool.NetworkTool;
import net.sunxiaolong.tool.captureinsureknowledge.tool.SnowFlakeIdGenerator;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @author 晓龙
 * @create 2018-08-07 上午11:42
 * @desc
 **/
public class CpicCapture implements Runnable {

    private CpicKnowledgeRepository knowledgeRepository;

    private int startPage;

    private int endPage;

    private String basePathUrlStr;

    private CountDownLatch doneSignal;

    public CpicCapture(CountDownLatch doneSignal, CpicKnowledgeRepository knowledgeRepository, int startPage, int endPage, String basePathUrlStr) {
        this.knowledgeRepository = knowledgeRepository;
        this.startPage = startPage;
        this.endPage = endPage;
        this.basePathUrlStr = basePathUrlStr;
        this.doneSignal = doneSignal;
    }

    public <T extends CpicKnowledge> List<T> getKnowLedge(String url) throws XPatherException {
        HtmlCleaner hc = new HtmlCleaner();
        List knowledges = new ArrayList();
        String cpicBaseUrlStr = "http://www.cpic.com.cn";
        String thisPageContent = NetworkTool.getStringFromUrl(url, null);
        TagNode thisPageTn = hc.clean(thisPageContent);
        String selectCententDivPath = "//div[@class='centent']";
        Object[] cententObjArray = thisPageTn.evaluateXPath(selectCententDivPath);
        for (Object cententObj : cententObjArray) {
            TagNode cententTn = (TagNode) cententObj;
            TagNode[] cententChildTagArray = cententTn.getChildTags();
            TagNode cententANode = cententChildTagArray[0];
            // 详情页面链接
            String thisHref = cpicBaseUrlStr + cententANode.getAttributeByName("href");
            // 问答详情页内容
            String contentPageString = NetworkTool.getStringFromUrl(thisHref, null);
            TagNode contentPageTn = hc.clean(contentPageString);

            String contentDivPath = "//div[@class='details-centent']";
            TagNode contentTn = (TagNode) contentPageTn.evaluateXPath(contentDivPath)[0];

            String titleH1Path = "//div[@class='details-top clearfix']/h1";
            TagNode titleTn = (TagNode) contentPageTn.evaluateXPath(titleH1Path)[0];

            String tagAPath = "//div[@class='tag']/a";
            Object[] aObjArray = contentPageTn.evaluateXPath(tagAPath);
            StringBuffer tagSb = new StringBuffer();
            for (Object aObj : aObjArray) {
                TagNode node = (TagNode) aObj;
                tagSb.append(node.getText().toString() + ",");
            }
            CpicKnowledge knowledge = new CpicKnowledge(SnowFlakeIdGenerator.nextId(),
                    titleTn.getText().toString(),
                    contentTn.getText().toString(),
                    tagSb.toString(),
                    thisHref,
                    basePathUrlStr
            );
            knowledges.add(knowledge);
        }
        return knowledges;
    }

    @Override
    public void run() {
        try {
            for (int i = startPage; i <= endPage; i++) {
//                String basePathUrlStr = "http://www.cpic.com.cn/zixun2/rsbxzs/";
                String url;
                if (i == 1) {
                    url = basePathUrlStr + "index.shtml";
                } else {
                    url = basePathUrlStr + "index_" + i + ".shtml";
                }
                List<CpicKnowledge> knowledges = getKnowLedge(url);
                knowledgeRepository.saveAll(knowledges);
                System.out.printf("第%d页导出完成\n", i);
            }
        } catch (XPatherException e) {
            e.printStackTrace();
        } finally {
            doneSignal.countDown();
        }
    }
}
